/**
* @author Ashish Gupta
* CVSSP, University of Surrey
* Copyright (c) 2010 Ashish Gupta
* All rights reserved.
*/
/**
 * 
 */
package genetic;

import java.util.Random;

import configuration.GenePoolConfiguration;

/**
 * @author Ashish
 *
 */
public class CrossOverMutation implements GeneticOperator {

	/* (non-Javadoc)
	 * @see genetic.GeneticOperator#operate(java.lang.Object)
	 */
	@Override
	public Object operate(Object genes) {
		return genes;
	}

	/* (non-Javadoc)
	 * @see genetic.GeneticOperator#operate(genetic.GeneMap)
	 */
	@Override
	public Object operate(GeneMap map) {
		return map;
	}

	/* (non-Javadoc)
	 * @see genetic.GeneticOperator#operate(genetic.GeneMap, configuration.GenePoolConfiguration)
	 */
	@Override
	public Object operate(GeneMap map, GenePoolConfiguration genePoolConfig) {
		return map;
	}

	/* (non-Javadoc)
	 * @see genetic.GeneticOperator#operate(genetic.GeneMap, genetic.GeneMap, configuration.GenePoolConfiguration)
	 */
	@Override
	public Object operate(GeneMap mapFather, GeneMap mapMother,
			GenePoolConfiguration genePoolConfig) {
		GeneMap mapChild = new GeneMap();
		mapChild.putAll(mapMother);
		int numKeysToMutate = (int) (genePoolConfig.getProportionKeysToMutateInCrossOverMutation() * mapMother.keys().size());
		Random rnd = new Random();
		int blockHeadIdxInMother = rnd.nextInt(mapMother.keys().size()-numKeysToMutate);
		int blockHeadIdxInFather = rnd.nextInt(mapFather.keys().size() - numKeysToMutate);
		for(int i = 0; i < numKeysToMutate; i++){
			int key = blockHeadIdxInMother + i;
			int value = mapFather.getKey(blockHeadIdxInFather+i);
			mapChild.put(key, value);
		}
		
		return mapChild;
	}

}
